package com.reading.controller;

import com.reading.feign.UserFeignClient;
import com.reading.mapper.DailyTasksMapper;
import com.reading.mapper.TaskMapper;
import com.reading.pojo.DailyShell;
import com.reading.pojo.ReadingTime;
import com.reading.pojo.Task;
import com.reading.pojo.TaskVo;
import com.reading.pojo.user.User;
import com.reading.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private DailyTasksMapper dailyTasksMapper;

    @RequestMapping("/findAll")
    public List<Task> findAl(Integer userId){
        return taskService.findAll(userId);
    }

    @RequestMapping("/updateOne")
    public void updateOne(@RequestBody TaskVo taskVo){
        taskService.updateOne(taskVo);
    }

    @RequestMapping("/receiveShell")
    public void receiveShell(@RequestBody DailyShell dailyShell){
        taskService.receiveShell(dailyShell);
    }

    @RequestMapping("/findOne")
    public Task findOne(Integer id){
        return taskService.findOne(id);
    }

    @RequestMapping("/updateAllStatus")
    public void updateAllStatus(){
        taskService.updateAllStatus();
    }

    @RequestMapping("/addTaskList")
    public void addTaskList(Integer id){
        taskService.addTaskList(id);
    }

    @RequestMapping("/getStartTime")
    public String getStartTime(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sdf.format(date);
        return startTime;
    }

    @RequestMapping("/getReadingTime")
    public String getReadingTime(@RequestBody ReadingTime readingTime){

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为LocalDateTime对象
        LocalDateTime start = LocalDateTime.parse(readingTime.getStartTime(), dtf);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format(date);
        LocalDateTime end = LocalDateTime.parse(endTime, dtf);
        Duration duration = Duration.between(start, end);
        int seconds = (int) duration.getSeconds();

        //修改该用户的阅读时间
        ReadingTime readingTime1 = new ReadingTime();
        readingTime1.setId(readingTime.getId());
        readingTime1.setNumber(seconds);
        dailyTasksMapper.updateReadingTime(readingTime1);
        return seconds+"";
    }

}
